// ACE_Message_Block.cpp : Defines the entry point for the console application.
//

#include <ace/Message_Block.h>
#include <ace/String_Base.h>
#include <ace/CDR_Stream.h>




#include <string>
using namespace std;

class DataClass
{
public:
	DataClass()
	{
	}
	void serialize(ACE_OutputCDR & cdr)
	{
		cdr.write_long(m_id);
		cdr.write_string(m_name);
	}
	void deserialize(ACE_InputCDR & cdr)
	{
		cdr.read_long(m_id);
		cdr.read_string(m_name);
	}
public:
	int m_id;
	ACE_CString m_name;
};

int main(int argc, char* argv[])
{
	printf("main entered.\n");

	DataClass* cls = new DataClass();
	cls->m_id = 128;
	cls->m_name = "test Class";
	ACE_Message_Block* block = new ACE_Message_Block((const char*)cls,sizeof(cls));

	printf("Class address <%p>.\n", cls);

	printf("Message Block rd_ptr <%p>.\n", block->rd_ptr());
	ACE_Message_Block* block2 = block->duplicate();
	printf("Message Block rd_ptr <%p>.\n", block2->rd_ptr());

	printf("Data Block <%p>.\n", block->data_block());
	printf("Data Block <%p>.\n", block2->data_block());

	block->release();
	block2->release();
	block2->release(); core dump test;

	ACE_OutputCDR out_cdr;
	out_cdr.write_boolean(ACE_CDR_BYTE_ORDER); // ACE_CDR_BYTE_ORDER is true by default in LITTLE_ENDIAN (windows/linux use little endian).
	cls->serialize(out_cdr);
	// send this message to another app
	//out_cdr.reset_byte_order(1);
	//out_cdr.begin();

	// The other app use this messageBlock;
	ACE_InputCDR in_cdr(out_cdr.begin());
	ACE_CDR::Boolean order;
	in_cdr.read_boolean(order);
	in_cdr.reset_byte_order(order);	// must re-order here
	DataClass testClass;
	testClass.deserialize(in_cdr);
	printf("Class id:<%d> name:<%s>",testClass.m_id,testClass.m_name.c_str());
	


	getchar();
	return 0;
}

